import Vue from 'vue'
import Vuex from 'vuex'
import { loginAPI } from '@/api/index.js'

Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    token: window.localStorage.getItem('token') || ''
  },
  mutations: {
    setToken (state, token) {
      state.token = token
      window.localStorage.setItem('token', token)
    }
  },
  actions: {
    async getUserList (store, val) {
      try {
        const res = await loginAPI(val)
        const token = res.data.body.token
        store.commit('setToken', token)
        return Promise.resolve(res)
      } catch (err) {
        return Promise.reject(err)
      }
    }
  },
  modules: {
  }
})
